﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/11/22 19:49:53
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace WCYCReader.StorageHelper
{
    public class FileHelper
    {
        /// <summary>
        /// 将Stream转成byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public byte[] StreamToBytes(Stream stream)
        {
            try
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
            catch { return null; }
        }

        /// <summary>
        /// 将 byte[] 转成 Stream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>

        public Stream BytesToStream(byte[] bytes)
        {
            try
            {
                Stream stream = new MemoryStream(bytes);
                return stream;
            }
            catch { return null; }
        }

        /// <summary>
        /// 获取文件的编码
        /// <para>返回null则表示是gb2312编码，其它则有相应编辑返回</para>
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public Encoding GetFileEncodingType(string filepath)
        {
            try
            {
                Stream stream = IsolatedStorageHelper.Instance.ReadFile(filepath);
                Encoding result = GetEncodingTypeByStream(stream);
                stream.Close();
                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取文件的编码
        /// <para>返回null则表示是gb2312编码，其它则有相应编辑返回</para>
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public Encoding GetEncodingTypeByStream(Stream stream)
        {
            /*
             * byte[] Unicode=new byte[]{0xFF,0xFE};
             * byte[] UnicodeBIG=new byte[]{0xFE,0xFF};
             * byte[] UTF8=new byte[]{0xEF,0xBB,0xBF};
             */
            Encoding result = null;
            try
            {
                using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8))
                {
                    byte[] b = reader.ReadBytes(3);

                    //编码类型 Coding=编码类型.ASCII;
                    if (b[0] >= 0xEF)
                    {
                        if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF)
                        {
                            result = Encoding.UTF8;
                        }
                        else if (b[0] == 0xFE && b[1] == 0xFF)
                        {
                            result = Encoding.BigEndianUnicode;
                        }
                        else if (b[0] == 0xFF && b[1] == 0xFE)
                        {
                            result = Encoding.Unicode;
                        }
                    }
                    reader.Close();
                }
            }
            catch
            {
                return null;
            }
            return result;

        }
    }
}
